package com.loongtech.bi.action.birealtime;

import com.loongtech.bi.action.TransferHelper;
import com.loongtech.bi.manager.realTime.AuanyDisplayByChannelManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.util.Time;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 
 * ClassName: AuanyDisplayByChannelBiAction
 * 
 * @Description: 实时数据-auany分渠道概况Action
 * @author liuchen
 * @date 2017-5-22 下午3:39:40
 */
@Controller
@Scope("prototype")
@RequestMapping("/realTime/auanyDisplayByChannel")
public class AuanyDisplayByChannelBiAction extends TransferHelper {
	private static final long serialVersionUID = 1L;
	@Resource
	private AuanyDisplayByChannelManager manager;

	@RequestMapping(value = "search.do", method = { RequestMethod.GET, RequestMethod.POST })
	public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String date,
			String callback) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {
			Date lookDate = Time.formatDate(date, "yyyy-MM-dd");
			Map<String, AuanyDisplayByChannelManager.AuanyChannelData> res = manager.updateDatas(lookDate);
            if (res != null && !res.isEmpty()) {
                for(AuanyDisplayByChannelManager.AuanyChannelData a : res.values()){
                    a.setChannelid(BiPlatformManager.getChannelNameById(a.getChannelid()));
                }
            }

            result.put("res", "0");
            result.put("data", res.values());
            printToJson(response, result, callback);
		} catch (Exception e) {
			e.printStackTrace();
			result.put("res", "1");
			result.put("msg", e.getMessage());
			printToJson(response, result, callback);
		}
	}
}
